package fun.yizhierha.tools.other.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.config.FileProperties;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.SecurityUtils;
import fun.yizhierha.common.utils.ValidList;
import fun.yizhierha.common.utils.file.ExcelUtils;
import fun.yizhierha.common.utils.file.FileUtil;
import fun.yizhierha.tools.other.domain.ToolLocalStorage;
import fun.yizhierha.tools.other.domain.vo.CreateToolLocalStorageVo;
import fun.yizhierha.tools.other.domain.vo.UpdateToolLocalStorageVo;
import fun.yizhierha.tools.other.domain.vo.RetrieveToolLocalStorageVo;
import fun.yizhierha.tools.other.mapper.ToolLocalStorageMapper;
import fun.yizhierha.tools.other.service.mapstruct.ToolLocalStorageMapstruct;
import fun.yizhierha.tools.other.service.ToolLocalStorageService;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/** generated by EH-Admin
* @author erha
* @date Sun Jan 01 17:11:43 CST 2023
**/
@Service
@RequiredArgsConstructor
public class ToolLocalStorageServiceImpl extends ServiceImpl<ToolLocalStorageMapper, ToolLocalStorage> implements ToolLocalStorageService{

    private final ToolLocalStorageMapstruct toolLocalStorageMapstruct;
    private final FileProperties fileProperties;

    @Override
    public PageUtils<ToolLocalStorage> list(RetrieveToolLocalStorageVo retrieveToolLocalStorageVo, Query.PageVo pageVo) {
        QueryWrapper<ToolLocalStorage> wrapper = new QueryWrapper<>();
        String realName = retrieveToolLocalStorageVo.getRealName();
        String name = retrieveToolLocalStorageVo.getName();
        String suffix = retrieveToolLocalStorageVo.getSuffix();
        Long startSize = retrieveToolLocalStorageVo.getStartBitSize();
        Long endSize = retrieveToolLocalStorageVo.getEndBitSize();
        Timestamp startCreateTime = retrieveToolLocalStorageVo.getStartCreateTime();
        Timestamp endCreateTime = retrieveToolLocalStorageVo.getEndCreateTime();
        Timestamp startUpdateTime = retrieveToolLocalStorageVo.getStartUpdateTime();
        Timestamp endUpdateTime = retrieveToolLocalStorageVo.getEndUpdateTime();

        if (realName != null){
            wrapper.like(ToolLocalStorage.COL_REAL_NAME,realName);
        }
        if (name != null){
            wrapper.like(ToolLocalStorage.COL_NAME,name);
        }
        if (suffix != null){
            wrapper.eq(ToolLocalStorage.COL_SUFFIX,suffix);
        }

        if (startSize != null && endSize != null){
            wrapper.between(ToolLocalStorage.COL_BIT_SIZE,startSize,endSize);
        }
        if (startCreateTime != null && endCreateTime != null){
            wrapper.between(ToolLocalStorage.COL_CREATE_TIME,startCreateTime,endCreateTime);
        }
        if (startUpdateTime != null && endUpdateTime != null){
            wrapper.between(ToolLocalStorage.COL_UPDATE_TIME,startUpdateTime,endUpdateTime);
        }

        IPage<ToolLocalStorage> iPage = baseMapper.selectPage(new Query<ToolLocalStorage>().getPage(pageVo), wrapper);
        return new PageUtils<>(iPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void save(CreateToolLocalStorageVo createToolLocalStorageVo, MultipartFile file) {
        // 1.检查文件大小
        FileUtil.checkSize(fileProperties.getMaxSize(),file.getSize());
        // 2.映射数据
        ToolLocalStorage toolLocalStorage = toolLocalStorageMapstruct.toToolLocalStorage(createToolLocalStorageVo);
        toolLocalStorage.setCreateTime(new Timestamp(new Date().getTime()));
        toolLocalStorage.setCreateBy(SecurityUtils.getCurrentUser().getUsername());
        toolLocalStorage.setBitSize(file.getSize());

        String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
        String type = FileUtil.getFileType(suffix);
        File uploadFile = FileUtil.upload(file, fileProperties.getPath().getPath() + type + File.separator);
        if (uploadFile == null) throw new BadRequestException("上传失败");

        toolLocalStorage.setRealName(uploadFile.getName());
        toolLocalStorage.setPath(uploadFile.getPath());
        toolLocalStorage.setSize(FileUtil.getSize(file.getSize()));
        toolLocalStorage.setSuffix(suffix);
        toolLocalStorage.setType(type);
        // 3.保存    
        this.save(toolLocalStorage);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void edit(ValidList<UpdateToolLocalStorageVo> updateToolLocalStorageVoList, List<BaseErrDto> errDtoList) {
        List<ToolLocalStorage> toUpdateToolLocalStorageList = new ArrayList<>();

        for (UpdateToolLocalStorageVo updateToolLocalStorageVo : updateToolLocalStorageVoList) {
            Long id = updateToolLocalStorageVo.getId();
            // 1.字段为UNI，需要不重复

            ToolLocalStorage toolLocalStorage = toolLocalStorageMapstruct.toToolLocalStorage(updateToolLocalStorageVo);
            toolLocalStorage.setUpdateTime(new Timestamp(new Date().getTime()));
            toolLocalStorage.setUpdateBy(SecurityUtils.getCurrentUser().getUsername());

            toUpdateToolLocalStorageList.add(toolLocalStorage);
        }

        // 2.更新
        this.updateBatchById(toUpdateToolLocalStorageList);
    }

    @Override
    public void remove(Set<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void download(HttpServletResponse response) {
        ExcelUtils.export(response,"存储管理信息表",this.list(), ToolLocalStorage.class);
    }

}